﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S3.DataModel;
using S3.Common;

namespace S3.Biz
{
    public class TimeBlockUnitPriceManager : CommonManager
    {
        #region Constants

        private static readonly string Avaible = TimeBlockUnitPriceStatusValue.Text[TimeBlockUnitPriceStatus.Avaiable];

        #endregion

        
        /// <summary>
        /// Get One instance TimeBlockUnitPrice
        /// </summary>
        /// <param name="shortName"></param>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
        public TimeBlockUnitPrice GetOne(string kindOfMachine)
        {
            //Check parameter
            if (string.IsNullOrEmpty(kindOfMachine)) return null;

            try
            {
                return _entities.TimeBlockUnitPrices.FirstOrDefault(a => a.TimingServiceType.ToLower() == kindOfMachine.ToLower() && a.Status == Avaible);
            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("TimeBlockUnitPriceManager::GetOne::" + kindOfMachine, ex, true);
                }
            }

            return null;
        }

        #region Save
        /// <summary>
        /// Save entry to SQL
        /// </summary>
        /// <param name="dailyEntry"></param>
        public void Save(TimeBlockUnitPrice input)
        {
            try
            {
                TimeBlockUnitPrice entry = null;
                //Check exist first
                // If existing then update else add new
                if (IsExist(input.TimingServiceType, ref entry))
                {
                    entry.FirstBlockTime = input.FirstBlockTime;
                    entry.FirstBlockUnitPrice = input.FirstBlockUnitPrice;
                    entry.NextBlockTime = input.NextBlockTime;
                    entry.NextBlockUnitPrice = input.NextBlockUnitPrice;
                    entry.TotalMoney = input.TotalMoney;
                    entry.ModifiedBy = Common.Constants.StaffName;
                    entry.ModifiedDate = DateTime.Now;

                }
                else
                {
                    //Add new
                    input.ModifiedBy = Common.Constants.StaffName;
                    input.ModifiedDate = DateTime.Now;
                    input.Status = TimeBlockUnitPriceStatusValue.Text[TimeBlockUnitPriceStatus.Avaiable];

                    _entities.AddToTimeBlockUnitPrices(input);
                    
                }

                //Submit change
                var result = _entities.SaveChanges();
                //ActionLogger.LogAction(ActionType.ChangeBlock, Constants.StaffName, DateTime.Now, new string[] { });

            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("TimeBlockUnitPriceManager::Save::" + input.TimingServiceType, ex, true);
                }
            }

        }
        #endregion

        /// <summary>
        /// Check existing or not
        /// </summary>
        /// <param name="deskName"></param>
        /// <param name="itemName"></param>
        /// <returns></returns>
        public bool IsExist(string kindOfMachine, ref TimeBlockUnitPrice entry)
        {
            //Check parameter
            if (string.IsNullOrEmpty(kindOfMachine)) return false;

            try
            {
                entry = _entities.TimeBlockUnitPrices.FirstOrDefault(a => a.Status == Avaible && a.TimingServiceType == kindOfMachine);
                return entry != null;
            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("TimeBlockUnitPriceManager::IsExist::" + entry.TimingServiceType, ex, true);
                }
            }

            return false;
        }
    }
}
